package com.syz.web.controller;


import com.syz.web.pojo.Course;
import com.syz.web.pojo.Student;
import com.syz.web.service.ICourseService;
import com.syz.web.service.Impl.CourseServiceImpl;

import com.syz.web.util.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/course")
public class CourseServlet extends HttpServlet {

    private ICourseService courseService = new CourseServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.setCharacterEncoding("UTF-8");

        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "getCourseUpdatePage":
                getStudentUpdatePage(req,resp);
                break;
            case "update":
                update(req,resp);
                break;    
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String credit = req.getParameter("credit");

        boolean success=courseService.update(id,name,credit);

        resp.sendRedirect(req.getContextPath() + "/course");
    }

    private void getStudentUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");

        Course course=null;
        course=courseService.getCourseUpdatePage(id);

        //把list数据放到req里面
        req.setAttribute("course", course);
        //转发到student_list.jsp页面进行展示
        req.getRequestDispatcher("Course/course_update.jsp").forward(req, resp);
        
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        String credit = req.getParameter("credit");

        boolean success=courseService.add(name,credit);

        resp.sendRedirect(req.getContextPath() + "/course");
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("CourseServlet.deleteById");
        String id = req.getParameter("id");
        boolean isSuccess = courseService.deleteById(Integer.parseInt(id));
        resp.sendRedirect(req.getContextPath() + "/course");
    }


    // /course?method=selectByPage&pageNo=2&pageSize=5
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CourseServlet.selectByPage");
        String pageNoStr = req.getParameter("pageNo");
        if (pageNoStr == null || pageNoStr.equals("")) {
            pageNoStr = "1";
        }
        String pageSizeStr = req.getParameter("pageSize");
        if (pageSizeStr == null || pageSizeStr.equals("")) {
            pageSizeStr = "5";
        }
        int pageNo = Integer.parseInt(pageNoStr);
        int pageSize = Integer.parseInt(pageSizeStr);

        PageInfo<Course> pageInfo = courseService.selectByPage(pageNo, pageSize);

        req.setAttribute("pageInfo", pageInfo);
        req.getRequestDispatcher("Course/course_list.jsp").forward(req, resp);
    }

}
